package youcaihuaoa.weetop.com.ui.main

import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import youcaihuaoa.weetop.com.R
import youcaihuaoa.weetop.com.databinding.AppFragmentHomeBinding
import youcaihuaoa.weetop.com.vm.main.HomeFragmentViewModel
import com.robot.base.base.activity.GLBaseFragment
import com.robot.base.common.api.GlBaseApi
import com.robot.base.configs.AppConstants
import com.robot.base.util.arouter.ARouterUtil
import com.robot.base.utils.pic.ImageLoaderUtils
import com.youth.banner.BannerConfig
import com.youth.banner.Transformer
import com.youth.banner.loader.ImageLoader
import me.goldze.mvvmhabit.BR
import youcaihuaoa.weetop.com.ui.adapter.HomeItemAdapter
import youcaihuaoa.weetop.com.ui.adapter.MyApplyAdapter

/**
 * 设备
 */
class HomeFragment : GLBaseFragment<AppFragmentHomeBinding, HomeFragmentViewModel>() {

    private lateinit var homeAdapter: HomeItemAdapter

    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_home

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        homeAdapter = HomeItemAdapter().apply {
            setOnItemClickListener { adapter, view, position ->
                if (homeAdapter.data[position].template_type == "99") {
                    ARouterUtil.goToActivityWithBundle(
                        AppConstants.Router.Main.A_TREND_APPLY_PROFILE,
                        Bundle().apply
                        {
                            putString(
                                AppConstants.BundleKey.ZDY_FORM_ID,
                                homeAdapter.data[position].zdy_form_id
                            )
                            putString(AppConstants.BundleKey.ID, homeAdapter.data[position].id)
                            putString(
                                AppConstants.BundleKey.TITLE,
                                homeAdapter.data[position].public_title
                            )
                            putString(
                                AppConstants.BundleKey.TYPE,
                                homeAdapter.data[position].template_type
                            )
                            putInt(
                                AppConstants.BundleKey.STATUS,
                                if (viewModel.isMyApplyTab.get() == true) {
                                    1
                                } else {
                                    2
                                }
                            )
                        })
                } else {
                    ARouterUtil.goToActivityWithBundle(
                        AppConstants.Router.Main.A_MY_APPLY_PROFILE,
                        Bundle().apply
                        {
                            putString(AppConstants.BundleKey.ID, homeAdapter.data[position].id)
                            putString(
                                AppConstants.BundleKey.TITLE,
                                homeAdapter.data[position].public_title
                            )
                            putString(
                                AppConstants.BundleKey.TYPE,
                                homeAdapter.data[position].template_type
                            )
                            putInt(
                                AppConstants.BundleKey.STATUS,
                                if (viewModel.isMyApplyTab.get() == true) {
                                    1
                                } else {
                                    2
                                }
                            )
                        })
                }
            }
        }
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = homeAdapter
        }
        viewModel.fetchBanner()
        viewModel.examineUserData()


        binding.banner.setImageLoader(object : ImageLoader() {
            override fun displayImage(context: Context, path: Any, imageView: ImageView) {
                ImageLoaderUtils.display(getContext(), imageView, path.toString())
            }

        })
        binding.banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        binding.banner.setBannerAnimation(Transformer.CubeOut);
        binding.banner.setDelayTime(5000);

    }


    override fun initViewObservable() {
        viewModel.uiEvent.bannerDataEvent.observe(this) {
            val imgs = it.map { "http://ych.hl2000.com" + it.images_path }
            val titles = it.map { it.title }
            binding.banner.setImages(imgs)
            binding.banner.setBannerTitles(titles)
            binding.banner.start();
        }

        viewModel.uiEvent.dataEvent.observe(this) {
            homeAdapter.type = viewModel.isMyApplyTab.get() ?: true
            homeAdapter.setList(it)
        }
    }
}